package com.lovo.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MQSConfig {

    //spring封装rabiitmq的代码 RabbitTemplate
//注入工厂（已经预加载）
    @Autowired
    CachingConnectionFactory connectionFactory;

    //rabbitmq模板
    @Bean(value = "rabbitTemplate")
    public RabbitTemplate rabbitTemplate() {
        return new RabbitTemplate(connectionFactory);
    }

    //订单 队列
    @Bean
    public Queue sellQueue() {

        return new Queue("sellQueue");
    }

    //订单商品
    @Bean
    public Queue sellGoodsQueue() {

        return new Queue("sellGoodsQueue");
    }

    //订单 交换机
    @Bean
    public DirectExchange directExchangeSell() {

        return new DirectExchange("directExchangeSell");
    }

    //订单商品
    @Bean
    public DirectExchange directExchangeSellGoods() {

        return new DirectExchange("directExchangeSellGoods");
    }


    // 订单 交换机与队列进行绑定
    @Bean
    public Binding sellQueueToDirectExchangeSell(Queue sellQueue, DirectExchange directExchangeSell) {

        return BindingBuilder.bind(sellQueue).to(directExchangeSell).with("sell.key");
    }

    //订单商品   交换机与队列进行绑定
    @Bean
    public Binding sellGoodsQueueToDirectExchangeSellGoods(Queue sellGoodsQueue, DirectExchange directExchangeSellGoods) {

        return BindingBuilder.bind(sellGoodsQueue).to(directExchangeSellGoods).with("sellgoods.key");
    }


}
